

from django.core.paginator import Paginator, InvalidPage, EmptyPage

from django.core.urlresolvers import reverse

from django.http import Http404

from django.shortcuts import render_to_response , render , get_object_or_404, HttpResponseRedirect

from django.template import RequestContext


from models import Fragrance, Paper, News , Contact
from forms import *
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

       
def default_render(request,extra_context={}, template_name='index.html'):

    
    templateVars = {}
    if extra_context != {}:
        templateVars.update(extra_context)

    return render( request , template_name , templateVars)

    
def index(request):
    
    extra_context = {}
    return default_render(request , extra_context = extra_context )
    
def verify(request):
    extra_context = {}
    return default_render(request , template_name='googlea0545ca95298fb6d.html', extra_context = extra_context )
    
    
def fragrance_view(request, id):
    fragrance_object = get_object_or_404(Fragrance, id=id)
    #if request.is_ajax():
    #  return HttpResponse(simplejson.dumps(fragrance_object), mimetype='application/javascript')
    return render_to_response('fragrance_view.html', {'fragrance_object':fragrance_object,}, context_instance=RequestContext(request))


#@login_required    
def fragrance_create(request):
    if request.method == 'POST':
        form = FragranceCreate(request.POST)
        if form.is_valid():
            item = form.save()
            return HttpResponseRedirect(reverse('fragrances:fragrance_view', kwargs={'id':item.id}))
    else:
        form = FragranceCreate()
    return render_to_response('fragrance_create.html', {'form': form, }, context_instance=RequestContext(request))





#@login_required    
def fragrance_edit(request, id=id):
    fragrance_object = get_object_or_404(Fragrance, id=id)
    #if request.user != fragrance_object.user:
    #   raise PermissionDenied
    if request.method == 'POST':
        form = FragranceEdit(request.POST, instance=fragrance_object)
        if form.is_valid():
            item = form.save()
            return HttpResponseRedirect(reverse('fragrances:fragrance_view', kwargs={'id':item.id}))
    else:
        form = FragranceEdit(instance=fragrance_object)
    return render_to_response('fragrance_edit.html', {'form': form, 'fragrance_object': fragrance_object}, context_instance=RequestContext(request))




def contact_us(request):

   
    #if request.user != fragrance_object.user:
    #   raise PermissionDenied
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            item = form.save()
            msg = "Thank you , your message has been sent "
            return default_render(request , template_name= "Contact.html" , extra_context ={"msg": msg })
    else:
        form = ContactForm()
    return default_render(request,  template_name ="Contact.html" , extra_context ={"form" : form} )

def fragrance_list(request):
    fragrance_set = Fragrance.objects.all() # .order_by('-date') 

    # return default_render(request , template_name ="Products.html" )
    return render_to_response('Products.html', {'fragrance_set':fragrance_set,}, context_instance=RequestContext(request))

     
def paper_view(request, id):
    paper_object = get_object_or_404(Paper, id=id)
    #if request.is_ajax():
    #  return HttpResponse(simplejson.dumps(paper_object), mimetype='application/javascript')
    return render_to_response('paper_view.html', {'paper_object':paper_object,}, context_instance=RequestContext(request))



#@login_required    
def paper_create(request):
    if request.method == 'POST':
        form = PaperCreate(request.POST)
        if form.is_valid():
            item = form.save()
            return HttpResponseRedirect(reverse('fragrances:paper_view', kwargs={'id':item.id}))
    else:
        form = PaperCreate()
    return render_to_response('paper_create.html', {'form': form, }, context_instance=RequestContext(request))





#@login_required    
def paper_edit(request, id=id):
    paper_object = get_object_or_404(Paper, id=id)
    #if request.user != paper_object.user:
    #   raise PermissionDenied
    if request.method == 'POST':
        form = PaperEdit(request.POST, instance=paper_object)
        if form.is_valid():
            item = form.save()
            return HttpResponseRedirect(reverse('fragrances:paper_view', kwargs={'id':item.id}))
    else:
        form = PaperEdit(instance=paper_object)
    return render_to_response('paper_edit.html', {'form': form, 'paper_object': paper_object}, context_instance=RequestContext(request))





def paper_list(request):
    paper_set = Paper.objects.all() # .order_by('-date')           
    return render_to_response('paper_list.html',{'paper_set': paper_set,}, context_instance=RequestContext(request))





def news_view(request, id):
    news_object = get_object_or_404(News, id=id)
    #if request.is_ajax():
    #  return HttpResponse(simplejson.dumps(news_object), mimetype='application/javascript')
    return render_to_response('news_view.html', {'news_object':news_object,}, context_instance=RequestContext(request))



#@login_required    
def news_create(request):
    if request.method == 'POST':
        form = NewsCreate(request.POST)
        if form.is_valid():
            item = form.save()
            return HttpResponseRedirect(reverse('fragrances:news_view', kwargs={'id':item.id}))
    else:
        form = NewsCreate()
    return render_to_response('news_create.html', {'form': form, }, context_instance=RequestContext(request))





#@login_required    
def news_edit(request, id=id):
    news_object = get_object_or_404(News, id=id)
    #if request.user != news_object.user:
    #   raise PermissionDenied
    if request.method == 'POST':
        form = NewsEdit(request.POST, instance=news_object)
        if form.is_valid():
            item = form.save()
            return HttpResponseRedirect(reverse('fragrances:news_view', kwargs={'id':item.id}))
    else:
        form = NewsEdit(instance=news_object)
    return render_to_response('news_edit.html', {'form': form, 'news_object': news_object}, context_instance=RequestContext(request))


def news_list(request):
    news_set = News.objects.all() # .order_by('-date')           
    return render_to_response('news_list.html',{'news_set': news_set,}, context_instance=RequestContext(request))


    
def fragrance_list1(request):
    fragrances = Fragrance.objects.all()
    
    
    paginator = Paginator(fragrances, 5) # Show 25 contacts per page

    page = request.GET.get("page")

    try:
        events = paginator.page(page)
    except  (PageNotAnInteger, TypeError):
    
        # If page is not an integer, deliver first page.
        events = paginator.page(1)
    except EmptyPage:
        # If page is out of range (e.g. 9999), deliver last page of results.
        events = paginator.page(paginator.num_pages)
        
    templateVars = {"contacts": events}
    return default_render(request,templateVars , "fragrances.html")    

